import {handleActions} from 'redux-actions';

import { NOTIFICATION_UNREAD_QUERY, NOTIFICATION_NEW_QUERY, NOTIFICATION_MARK_READED } from '../actions/ActionTypes.js'

const notification = handleActions({
  [NOTIFICATION_UNREAD_QUERY.REQUEST](state, action) {
    return { ...state };
  },
  [NOTIFICATION_UNREAD_QUERY.SUCCESS](state, action) {
    const notification = action.payload;
    const isHavingUnread = (notification instanceof Array && notification.length > 0);
    return { ...state, data: action.payload,  isHavingUnread};
  },
  [NOTIFICATION_UNREAD_QUERY.FAILURE](state, action) {
    return { ...state, err: action.err };
  },
  [NOTIFICATION_NEW_QUERY.REQUEST](state, action) {
    return { ...state };
  },
  [NOTIFICATION_NEW_QUERY.SUCCESS](state, action) {
    return { ...state };
  },
  [NOTIFICATION_NEW_QUERY.FAILURE](state, action) {
    return { ...state, err: action.err };
  },
  [NOTIFICATION_MARK_READED.REQUEST](state, action) {
    return { ...state };
  },
  [NOTIFICATION_MARK_READED.SUCCESS](state, action) {
    return { ...state };
  },
  [NOTIFICATION_MARK_READED.FAILURE](state, action) {
    return { ...state, err: action.err };
  },

}, {
  isHavingUnread:false,
  data:[],
});

export default notification;
